package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import cn.wolfcode.wolf2w.util.RedisKeys;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public StrategyStatisVO statisVo(String sid) {

        String join = RedisKeys.STRATEGY_STATIS_VO.join(sid);

        StrategyStatisVO vo = null;
        if (template.hasKey(join)){
            String str = template.opsForValue().get(join);
            vo = JSON.parseObject(str, StrategyStatisVO.class);
        }else {
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            vo.setStrategyId(strategy.getId());
            BeanUtils.copyProperties(strategy, vo);
            template.opsForValue().set(join, JSON.toJSONString(vo));
        }

        return vo;
    }

    @Override
    public void increment(String id, int num) {

        String join = RedisKeys.STRATEGY_STATIS_VO.join(id);
        StrategyStatisVO vo = null;
        if (template.hasKey(join)){
            String str = template.opsForValue().get(join);
            vo = JSON.parseObject(str, StrategyStatisVO.class);
            vo.setViewnum(vo.getViewnum()+ num);
        }else {
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(id);
            strategy.setViewnum(strategy.getViewnum() + num);
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
        }

        template.opsForValue().set(join,JSON.toJSONString(vo));

    }

    @Override
    public void increaseReplynum(String sid, int num) {
        StrategyStatisVO vo = this.statisVo(sid);
        vo.setReplynum(vo.getReplynum() + 1);
        String join = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        template.opsForValue().set(join, JSON.toJSONString(vo));
    }

    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {

        String join = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());

        template.opsForValue().set(join, JSON.toJSONString(vo));

    }

    @Override
    public boolean strategyThumbup(String sid, String uid) {

        String key = RedisKeys.STRATEGY_THUMBUP.join(uid, sid);

        if (template.hasKey(key)){

            return false;

        }

        StrategyStatisVO vo = this.statisVo(sid);

        vo.setThumbsupnum(vo.getThumbsupnum() + 1);

        this.setStrategyStatisVO(vo);

        Date date = new Date();

        Date endDate = DateUtil.getEndDate(date);

        long between = DateUtil.getDateBetween(date, endDate);

        template.opsForValue().set(key, "1", between, TimeUnit.SECONDS);

        return true;
    }

    @Override
    public boolean isEisit(Strategy strategy) {

        String key = RedisKeys.STRATEGY_STATIS_VO.join(strategy.getId());

        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVoByPattern(String s) {

        List<StrategyStatisVO> list = new ArrayList<>();

        String keyPattern = RedisKeys.STRATEGY_STATIS_VO.join(s);

        Set<String> keys = template.keys(keyPattern);

        if (keys != null && keys.size() > 0){

            for (String key : keys) {

                String voStr = template.opsForValue().get(key);

                list.add(JSON.parseObject(voStr,StrategyStatisVO.class));
            }
        }

        return list;
    }
}
